package com.cssw.fyzb.web.controller;


import com.cssw.fyzb.base.web.context.UserContext;
import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.web.dto.FuncIdDto;
import com.cssw.fyzb.web.dto.SaveFuncDto;
import com.cssw.fyzb.web.dto.UpdateFuncDto;
import com.cssw.fyzb.web.service.FuncService;
import com.cssw.fyzb.web.util.SplitUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@ApiModel("菜单相关接口")
@RestController
@RequestMapping("/func")
public class FuncController {

    @Autowired
    FuncService funcService;

    @ApiOperation(value = "查询菜单列表", response = AjaxPage.class)
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "funcName", value = "菜单名称", paramType = "query", dataType = "string", required = false),
            @ApiImplicitParam(name = "funcCode", value = "菜单编码", paramType = "query", dataType = "string", required = false),
            @ApiImplicitParam(name = "funcType", value = "菜单类型", paramType = "query", dataType = "string", required = false),
            @ApiImplicitParam(name = "status", value = "状态", paramType = "query", dataType = "string", required = false),

    })
    public AjaxPage list(int page, int limit, String funcName, String funcCode, String funcType, String status) {
        Map param = new HashMap();
        param.put("funcName", funcName);
        param.put("funcCode", funcCode);
        param.put("funcCode", funcCode);
        param.put("status", status);
        param.put("funcType", funcType);
        return funcService.list(page, limit, param);
    }

    @ApiOperation(value = "新增菜单", response = AjaxData.class)
    @PostMapping(value = "/save")
    public AjaxData save(@Valid SaveFuncDto dto, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        return funcService.save(dto.getSupFuncId(), dto.getFuncName(), dto.getFuncCode(), dto.getFuncIcon(), dto.getFuncType(),
                dto.getFuncUrl(), dto.getStatus(), dto.getSort(), UserContext.getUserId());
    }

    @ApiOperation(value = "修改菜单", response = AjaxData.class)
    @PostMapping(value = "/update")
    public AjaxData update(@Valid UpdateFuncDto dto, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        return funcService.update(dto.getFuncId(), dto.getSupFuncId(), dto.getFuncName(), dto.getFuncCode(), dto.getFuncIcon(),
                dto.getFuncType(), dto.getFuncUrl(), dto.getStatus(), dto.getSort(), UserContext.getUserId());
    }

    @ApiOperation(value = "删除菜单", response = AjaxData.class)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxData delete(@Valid FuncIdDto dto, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        return funcService.delete(Long.parseLong(dto.getFuncId()));
    }

    @ApiOperation(value = "批量启用菜单", response = AjaxData.class)
    @RequestMapping(value = "/batchStart", method = RequestMethod.POST)
    public AjaxData batchStart(@Valid FuncIdDto dto, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        List<Long> funcIds = SplitUtil.splitToLongList(dto.getFuncId());
        return funcService.batchStart(funcIds, UserContext.getUserId());
    }

    @ApiOperation(value = "批量停用菜单", response = AjaxData.class)
    @RequestMapping(value = "/batchStop", method = RequestMethod.POST)
    public AjaxData batchStop(@Valid FuncIdDto dto, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        List<Long> funcIds = SplitUtil.splitToLongList(dto.getFuncId());
        return funcService.batchStop(funcIds, UserContext.getUserId());
    }

    @ApiOperation(value = "启用菜单", response = AjaxData.class)
    @RequestMapping(value = "/start", method = RequestMethod.POST)
    public AjaxData start(@Valid FuncIdDto dto, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        return funcService.start(Long.parseLong(dto.getFuncId()), UserContext.getUserId());
    }


    @ApiOperation(value = "停用菜单", response = AjaxData.class)
    @RequestMapping(value = "/stop", method = RequestMethod.POST)
    public AjaxData stop(@Valid FuncIdDto dto, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        return funcService.stop(Long.parseLong(dto.getFuncId()), UserContext.getUserId());
    }

    @ApiOperation(value = "查询菜单树列表", response = AjaxData.class)
    @RequestMapping(value = "/getTree", method = RequestMethod.GET)
    public AjaxData getTree() throws Exception {
        return funcService.getTree();
    }
}
